﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Homework7_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        bool isRY(int year)//闰年
        {
            return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
        }
        bool Check(string temp)
        {
            int year = Convert.ToInt32(temp.Substring(6, 4));
            int month = Convert.ToInt32(temp.Substring(10, 2));
            int day = Convert.ToInt32(temp.Substring(12, 2));

            if (month <= 0 || month >= 13 || day <= 0) { 
                return false; 
            }   //年月份非法
            int highMonth = 0;

            if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                highMonth = 31; 
            }
            else if (month == 2)
            {
                if (isRY(year)) highMonth = 29;
                else highMonth = 28;
            }
            else highMonth = 30;

            return day <= highMonth;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string check = @"[\d]{17}[0-9|X]";
            string temp = textBox1.Text;
            Regex regex = new Regex(check);
            if (temp.Length != 18) {
                label2.Text = "身份证号长度错误";
            }
            else if (!regex.IsMatch(temp))
            {
                label2.Text = "身份证号格式错误";
            }
            else if (!Check(temp)) {
                label2.Text = "身份证年月日非法"; 
            }
            else label2.Text = "验证成功";

        }
    }
}
